package pl.edu.agh.soa.monitoring.router.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPRuntime;

import pl.edu.agh.soa.monitoring.cep.EventInfo;
import pl.edu.agh.soa.monitoring.router.IDynamicRouter;
import pl.edu.agh.soa.monitoring.router.entry.DynamicRouterEntry;

public class DynamicRouter implements IDynamicRouter {
	
	private Map<String, List<DynamicRouterEntry>> routingMap;
	
	private EPAdministrator epAdministrator;

    private EPRuntime epRuntime;
	
	public DynamicRouter(EPAdministrator epAdministrator, EPRuntime epRuntime){
		routingMap = new HashMap<String, List<DynamicRouterEntry>>();
		this.epAdministrator = epAdministrator;
		this.epRuntime = epRuntime;
	}
	
	public void acquireMetric(String statementID) {
		// TODO Auto-generated method stub
	}

	public void addAddresses(String statementID, DynamicRouterEntry entry) {
		//pobieramy liste adresow na dany statement
		List<DynamicRouterEntry> tempEntryList = routingMap.get(statementID);
		if (tempEntryList == null){
			tempEntryList = new ArrayList<DynamicRouterEntry>();
			tempEntryList.add(entry);
			routingMap.put(statementID, tempEntryList);
		}else{
			tempEntryList.add(entry);
		}
	}

	public void disableLogging() {
		// TODO Auto-generated method stub
		
	}

	public void disableMetric(String statementID) {
		// TODO Auto-generated method stub
		
	}

	public void enableLogging() {
		// TODO Auto-generated method stub
		
	}

	public void enableMetric(String statementID) {
		// TODO Auto-generated method stub
		
	}

	/* 
	 *Tutaj powinna sie odbyc procedura wyslania eventu danemu gosciowi poprzez 
	 *SenderEndpoint na wskazany adres
	 */
	public void eventCatched(String statementID, EventInfo eventInfo) {
		System.out.println("Event catched: " + statementID + "\t," + eventInfo);
	}

	public Map<String, List<DynamicRouterEntry>> getRoutingMap() {
		return routingMap;
	}

	public void removeAddress(String statementID, DynamicRouterEntry entry) {
		//pobieramy liste adresow na dany statement
		List<DynamicRouterEntry> tempEntryList = routingMap.get(statementID);
		if (tempEntryList == null){
			//nothing to do
		}else{
			//znalezlismy entry ktore trzeba przefiltrowac i pousuwac odpowiednie wpisy
			for(DynamicRouterEntry e: tempEntryList){
				
				if (e.getStatementID().equals(statementID)){
					//znalezlismy nasze entry do przefiltrowania
					Map<String, List<String>> entriesToDelete = entry.getCallbackAddresses();
					
					for(String callbackAddress: e.getCallbackAddresses().keySet()){
						List<String> accessProtocolsToDelete = entriesToDelete.get(callbackAddress);
						if (accessProtocolsToDelete != null){
							//znalezlismy odbiorce do usuniecia danego rodzaju protokolu dostepu
							List<String> originalProtocols = e.getCallbackAddresses().get(callbackAddress);
							for(String protocolToDelete : accessProtocolsToDelete){
								//iterujemy po protokolach przekazanych jako parametr i usuwamy je z tablicy routingu
								originalProtocols.remove(protocolToDelete);
							}
						}
					}
					break;
				}
			}
		}
	}

	public void removeAddresses(String statementID) {
		//pobieramy liste adresow na dany statement
		List<DynamicRouterEntry> tempEntryList = routingMap.get(statementID);
		if (tempEntryList == null){
			//nothing to do
		}else{
			//znalezlismy entry ktore trzeba przefiltrowac i pousuwac odpowiednie wpisy
			for(DynamicRouterEntry e: tempEntryList){
				
				if (e.getStatementID().equals(statementID)){
					tempEntryList.remove(e);
					break;
				}
			}
		}
	}

}
